import { saveWrokStartReport } from './workStartReport.config.js'
import { vendorsFmtDate, vendorsDanger, } from 'utils/config.js';
import { getPrintCodeByType } from 'utils/netRequest.js';
import moment from 'moment';

let tenantId;
let cookieStr = document.cookie.split(';').find(cur => cur.indexOf('tenantid=') != -1)
if (cookieStr) {
    tenantId = cookieStr.split('=')[1];
}

tenantId = tenantId || 'x61tnmfx';


export default function buttonClick(params) {
    console.log(params)
    switch (params.key) {
        case 'listadd':
            this.props.router.push({ pathname: '/card', state: { cardState: 'add', pk: '' } })
            break;
        case 'save':
            let validateFormResult = this.props.form.validateForm();
            if (validateFormResult.length != 0) {
                vendorsDanger({ 'text': validateFormResult.join(',') + '为必输项' });
                return;
            }
            let workStartCheckVOS = this.state.workStartCheckVOS;
            let validateTableResult = [];
            console.log(workStartCheckVOS);
            workStartCheckVOS.forEach((cur, index) => {
                if (!cur.qualification) {
                    validateTableResult.push(index + 1);
                }
            })
            if (validateTableResult.length != 0) {
                vendorsDanger({ 'text': `请选择${validateFormResult.join(',')}行表体的结果` });
                return;
            }
            let formData = this.props.form.collectFormValue();
            formData.workStartCheckVOS = workStartCheckVOS;
            formData["billType"] = "KGBG";
            formData['startdate'] = moment(formData['startdate']).valueOf();
            formData.pkTenant = tenantId;
            saveWrokStartReport(formData,
                res => {
                    let { result, resultCode } = res.data;
                    if (resultCode == 'ok') {
                        let assignFlag = this.props.approve.initApproveModal(result); // 指派
                        if (assignFlag) {
                            this.setState({
                                cardState: 'browse',
                                pk: result.pkWorkstartreport,
                            })
                        }
                    } else {

                    }
                },
                error => {
                    console.log(error);
                }
            );
            break;
        case 'cancel':
            this.props.router.replace('/list');
            break;
        case 'add':
            this.setState({
                cardState: 'add',
                workStartCheckVOS: [],
                pk: '',
            })
            this.props.form.clearFormValue({});
            break;
        case 'attachment':
            this.ygcUpload.open();
            break;
        case 'listattachment':
            if (this.state.selectRows.length == 0) {
                this.props.vendorsWarning({ text: '请选择数据!' });
                return;
            }
            this.ygcUpload.open();
            break;
        case 'listprint':
            if (this.state.selectRows.length == 0) {
                this.props.vendorsWarning({ text: '请选择数据!' });
                return;
            }
            getPrintCodeByType({
                tenantId: tenantId,
                billType: 'KGBG',
                pkBill: this.state.selectRows[0]['pkWorkstartreport'],
                // pkProcess: this.state.pk,
            })
            break;
        case 'print':
            getPrintCodeByType({
                tenantId: tenantId,
                billType: 'KGBG',
                pkBill: this.state.pk,
                // pkProcess: this.state.pk,
            })
            break;
        case 'approve':
            this.props.passreject.initPassRejectModal({ record: this.state.formData, page: 'card', primaryKey: 'pkWorkstartreport' });
            break;
        case 'refresh':
            if (this.props.location.pathname == "/card") {
                this.getPageData();
            } else {
                let { selectedS } = this.props.search.getSearchData();
                this.searchBtnClick.call(this, selectedS, { type: 'refresh' });
            }
            break;
        case 'approveDes':
            this.props.approveHistory.initModal({ data: this.state.formData, primaryKey: 'pkWorkstartreport' });
            break;
        case 'listapproveDes':
            this.props.approveHistory.initModal({ data: this.state.selectRows[0], primaryKey: 'pkWorkstartreport' });
            break;
        default:
            break;
    }
}

